﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Globalization;
using System.Windows.Forms;

namespace Smart
{
    public static class Methods
    {
        public static decimal ToCurrency(this string deger)
        {
            if (!string.IsNullOrEmpty(deger))
            {
                //NumberFormatInfo LocalFormat = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
                //string sembol = LocalFormat.NumberDecimalSeparator;
                //if (sembol == ".")
                //{
                //  return Convert.ToDecimal(deger.Replace('.', ','));
                //}
                //else
                //{
                //  return Convert.ToDecimal(deger.Replace(',', '.'));
                //}
                return Convert.ToDecimal(deger.Replace('.', ','));
            }
            else
                return 0;
        }

        public static string ToFullSqlString(this DateTime tarih)
        {
            return string.Format("{0}-{1}-{2} {3}:{4}:{5}", tarih.Year, tarih.Month, tarih.Day, tarih.Hour, tarih.Minute, tarih.Second);
        }

        public static string RemoveTurkishCharacter(this string deger)
        {
            return deger.ToUpper().Replace('İ', 'I').Replace('Ö', 'O').Replace('Ü', 'U').Replace('Ş', 'S').Replace('Ğ', 'G').Replace('Ç', 'C');
        }

        public static string ToSqlString(this DateTime tarih)
        {
            return string.Format("{0}-{1}-{2} 00:00:00", tarih.Year, tarih.Month, tarih.Day);
        }

        public static DateTime ToFullDateTime(this string tarih)
        {
            string[] date = tarih.Split(' ')[0].Split('-');
            string[] saat = tarih.Split(' ')[1].Split(':');
            return new DateTime(int.Parse(date[0]), int.Parse(date[1]), int.Parse(date[2]), int.Parse(saat[0]), int.Parse(saat[1]), int.Parse(saat[2]));
        }

        public static DateTime ToDateTime(this string tarih)
        {
            string[] date = tarih.Split(' ')[0].Split('-');
            return new DateTime(int.Parse(date[0]), int.Parse(date[1]), int.Parse(date[2]));
        }

        public static string StartupPath()
        {
            return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
        }

        public static DbType ToDbType(this Type type)
        {
            string typeName = type.FullName;
            switch (typeName)
            {
                case "System.String":
                    return DbType.String;
                case "System.Boolean":
                    return DbType.Boolean;
                case "System.Int16":
                    return DbType.Int16;
                case "System.UInt16":
                    return DbType.UInt16;
                case "System.Int32":
                    return DbType.Int32;
                case "System.UInt32":
                    return DbType.UInt32;
                case "System.DateTime":
                    return DbType.DateTime;
                case "System.Decimal":
                    return DbType.Decimal;
                case "System.Double":
                    return DbType.Double;
                case "System.Int64":
                    return DbType.Int64;
                case "System.UInt64":
                    return DbType.UInt64;
                case "System.Byte":
                    return DbType.Byte;
                case "System.Char":
                    return DbType.Byte;
                case "System.Guid":
                    return DbType.Guid;
                default:
                    if (type.IsEnum)
                    {
                        return DbType.Int32;
                    }
                    return DbType.Object;
            }
        }

        public static T SelectedItem<T>(this DataGrid ctrl)
        {
            var cell = ctrl.CurrentCell;

            var list = (IList<T>)ctrl.DataSource;

            if (list == null)
                return default(T);
            if (list.Count == 0)
                return default(T);

            return list[cell.RowNumber];
        }

        private static void AddColumn(this GridColumnStylesCollection list, string header, string columnName, int width)
        {
            list.Add(
                new DataGridTextBoxColumn
                {
                    HeaderText = header,
                    MappingName = columnName,
                    Width = width
                });
            return;
        }

        public static void SetColumnStyles<T>(this DataGrid ctrl, T data, params ColumnStyle[] column) where T : class
        {
            var ts = new DataGridTableStyle();
            ts.MappingName = data.GetType().Name;

            for (int i = 0; i < column.Length; i++)
            {
                var style = column[i];
                ts.GridColumnStyles.AddColumn(style.Header, style.Column, style.Width);
            }

            ctrl.TableStyles.Clear();
            ctrl.TableStyles.Add(ts);
        }

        public static string ToSqlTypeString(this DbType type)
        {
            switch (type)
            {
                case DbType.DateTime:
                    return "datetime";
                case DbType.Int32:
                case DbType.UInt32:
                    return "int";
                case DbType.Int16:
                case DbType.UInt16:
                    return "smallint";
                case DbType.String:
                    return "nvarchar";
                case DbType.StringFixedLength:
                    return "nchar";
                case DbType.Boolean:
                    return "bit";
                case DbType.Object:
                    return "image";
                case DbType.Int64:
                    return "bigint";
                case DbType.UInt64:
                    return "bigint";
                case DbType.Byte:
                    return "tinyint";
                case DbType.Decimal:
                    return "money";
                case DbType.Double:
                    return "float";
                case DbType.Guid:
                    return "uniqueidentifier";
                default:
                    throw new NotSupportedException(
                        string.Format("Unable to determine convert DbType '{0}' to string", type.ToString()));
            }
        }

        public static T CastTo<T>(this object o, T t)
        {
            return (T)o;
        }

        public static string GetDescription(this Enum currentEnum)
        {
            string description = string.Empty;
            DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(currentEnum.GetType().GetField(currentEnum.ToString()), typeof(DescriptionAttribute));
            if (da != null)
            {
                description = da.Description;
            }
            else
            {
                description = currentEnum.ToString();
            }
            return description;
        }

        public static bool Compare(this byte[] dizi, byte[] target)
        {
            for (int i = 0; i < dizi.Length; i++)
            {
                if (dizi[i] != target[i])
                {
                    return false;
                }
            }
            return true;
        }
    }
}
